package gui;

import java.awt.RenderingHints;
import java.util.LinkedList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.LogarithmicAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

import tool.Complex;
import tool.Pair;

public class MainForm extends JFrame{

	private static final long serialVersionUID = -3728756035324658823L;
	private Integer WIDTH = 400;
	private Integer HEIGHT = 500;
	
	private static MainForm instance;
	
	private JFreeChart chart = null;
	private JFreeChart scatterChart = null;
	
	private String titleChart = "Transmitancja";
	private String xAxisLabel = "X";
	private String yAxisLabel = "Y";
	private PlotOrientation orientation = PlotOrientation.VERTICAL;
	private Boolean showLegend = true;
	private Boolean useTooltips = true;
	private Boolean urls = false;
	private List<Pair> list = new LinkedList<Pair>();
	private List<Complex> scatterList = new LinkedList<Complex>();
	
	public static MainForm getInstance(){
		if (instance == null)
			instance = new MainForm();
		return instance;
	}
	
	private MainForm(){
		this.setVisible(true);
		this.setSize(HEIGHT, WIDTH);
		this.add(new ParametersPanel());
	}
	
	public void createChart(){
		XYSeries series = getSeries();
		XYSeriesCollection seriesCollection = new XYSeriesCollection();
		seriesCollection.addSeries(series);
		generateChart(seriesCollection);
	}
	
	private XYSeries getSeries(){
		XYSeries series = new XYSeries("transmitancja");
		for(int i=0; i<list.size(); i++){
			Pair pair = list.get(i);
			series.add(Math.log10(pair.getX()), 20*Math.log10(Math.abs(pair.getY().getReal())));
		}
		return series;
	}
	
	private void generateChart(XYSeriesCollection dataset){
		chart = null;
		chart = ChartFactory.createXYLineChart(titleChart, xAxisLabel, yAxisLabel, dataset, 
				orientation, showLegend, useTooltips, urls);
	}
	
	private void createFrame() {
		while(true){
			if(chart != null && scatterChart != null){
			    break;
			}
		}
		JFrame frame = new JFrame();
		final ChartPanel chartPanel = new ChartPanel(chart);
		final XYPlot plot = chart.getXYPlot();
        final NumberAxis rangeAxis = new NumberAxis("|H(jw)|db");
        final NumberAxis domainAxis = new LogarithmicAxis("Log(x)");
        plot.setDomainAxis(domainAxis);
        plot.setRangeAxis(rangeAxis);
        
		 final ChartPanel scatterPanel = new ChartPanel(scatterChart, true);
		 scatterPanel.setPreferredSize(new java.awt.Dimension(500, 270));
		 scatterPanel.setMinimumDrawHeight(10);
		 scatterPanel.setMaximumDrawHeight(2000);
		 scatterPanel.setMinimumDrawWidth(20);
		 scatterPanel.setMaximumDrawWidth(2000);
		
		JTabbedPane tabbedPane = new JTabbedPane();	
		tabbedPane.addTab("Wykres filtru", chartPanel);
		tabbedPane.addTab("Bieguny", scatterPanel);
		tabbedPane.setVisible(true);
		frame.setSize(700, 700);
	    frame.add(tabbedPane);	
	    frame.setVisible(true);
		
	}
	
	private JPanel createPanel(){
		return new ChartPanel(chart);
	}
	
	public void drawChart(){
		createChart();
		this.add(createPanel());
	}
	
	public void setList(List<Pair> resList) {
		this.list = resList;
	}
	
	public void setScatterList (List<Complex> resList){
		this.scatterList = resList;
	}
	
	public void createPolesScatter() {
		final NumberAxis domainAxis = new NumberAxis("Real");
        domainAxis.setAutoRangeIncludesZero(false);
        final NumberAxis rangeAxis = new NumberAxis("Imag");
        rangeAxis.setAutoRangeIncludesZero(false);
        scatterChart = ChartFactory.createScatterPlot("Bieguny", "Real", "Imag", getScatterData(), PlotOrientation.VERTICAL,
        		false, false, false);
        scatterChart.getRenderingHints().put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        createFrame();
	}
	
	private XYDataset getScatterData() {
		XYSeriesCollection xySeriesCollection = new XYSeriesCollection();
        XYSeries series = new XYSeries("GRAPH");
		for (Complex comp : scatterList) {
//			System.out.println(comp.toString());
			series.add(comp.getReal(), comp.getImag());
		}
		xySeriesCollection.addSeries(series);
		return xySeriesCollection;
	}
}
